//
//  VehicleInfoCell.m
//  CarLife
//
//  Created by yu on 2017/10/30.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "VehicleInfoCell.h"

@interface VehicleInfoCell ()<UITextFieldDelegate>

@end

@implementation VehicleInfoCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.sBtn.selected = YES;
    self.platenoTF.delegate = self;
    self.vinTF.delegate = self;
    self.engineTF.delegate = self;

//    self.platenoTF.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
    
    UITapGestureRecognizer *cityTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cityLabelTapAction)];
    [self.cityLabel addGestureRecognizer:cityTap];
}

// 所有字母一律转为大写
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (string.length == 0 ){return YES;}
    
    char commitChar = [string characterAtIndex:0];
    if (commitChar > 96 && commitChar < 123) {
        NSString *uppercaseString = string.uppercaseString;
        NSString *str1 = [textField.text substringToIndex:range.location];
        NSString *str2 = [textField.text substringFromIndex:range.location];
        textField.text = [NSString stringWithFormat:@"%@%@%@",str1,uppercaseString,str2].uppercaseString;
        return NO;
        
    }
    return YES;
}

- (IBAction)vehicleTypeAction:(UIButton *)btn {
    switch (btn.tag) {
        case 1:
            self.bBtn.selected = NO;
            self.sBtn.selected = YES;
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(showVehicleTypeAction:)]) {
                [self.delegate showVehicleTypeAction:@"02"];
            }
            break;
        case 2:
            {
                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"暂不支持查询大车型" message:nil delegate:nil cancelButtonTitle:@"确 定" otherButtonTitles:nil, nil];
                [alertView show];
//                self.bBtn.selected = YES;
//                self.sBtn.selected = NO;
//
//                if (self.delegate && [self.delegate respondsToSelector:@selector(showVehicleTypeAction:)]) {
//                    [self.delegate showVehicleTypeAction:@"01"];
//                }
            }
            
            break;
        default:
            break;
    }
}

- (IBAction)showProvinceAbbrAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(showProvinceAbbrAction)]) {
        [self.delegate showProvinceAbbrAction];
    }
}

- (IBAction)showLicenseExampleAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(showLicenseExampleAction)]) {
        [self.delegate showLicenseExampleAction];
    }
}

- (void)cityLabelTapAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(showCityPageAction)]) {
        [self.delegate showCityPageAction];
    }
}

+ (instancetype)initTableViewCellWith:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
    NSString *identifier;
    NSInteger index = 0;
    switch (indexPath.row) {
        case 0:
            identifier = @"VehicleInfoCell0";
            index = 0;
            break;
        case 1:
            identifier = @"VehicleInfoCell1";
            index = 1;
            break;
        case 2:
            identifier = @"VehicleInfoCell2";
            index = 2;
            break;
        case 3:
            identifier = @"VehicleInfoCell3";
            index = 3;
            break;
        case 4:
            identifier = @"VehicleInfoCell4";
            index = 4;
            break;
        default:
            break;
    }
    VehicleInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"VehicleInfoCell" owner:self options:nil] objectAtIndex:index];
    }
    return cell;
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
